package com.example.ykbboom.universitytransportation.Activity;

import android.os.Bundle;
import android.os.Handler;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.example.ykbboom.universitytransportation.MyApplication;
import com.example.ykbboom.universitytransportation.POJO.MyRequestDetailItem;
import com.example.ykbboom.universitytransportation.R;
import com.example.ykbboom.universitytransportation.Util.NetUtil;
import com.example.ykbboom.universitytransportation.Util.SharedPrefUtil;
import com.example.ykbboom.universitytransportation.View.DividerItemDecoration;
import com.example.ykbboom.universitytransportation.View.MyRequestDetailAdapter;

import java.util.ArrayList;
import java.util.List;

public class MyRequestActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    private Toolbar mToolbar;
    private RecyclerView recyclerView;
    private List<MyRequestDetailItem>list = new ArrayList<>();
    private MyRequestDetailAdapter adapter;
    private SwipeRefreshLayout swipeRefreshLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_request);
        initView();
    }

    @Override
    public void initView() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar_my_request);
        setSupportActionBar(mToolbar);
        ActionBar actionBar = getSupportActionBar();
        if(actionBar!=null){
            actionBar.setHomeButtonEnabled(true); //设置返回键可用
            actionBar.setDisplayHomeAsUpEnabled(true);//显示HomeAsUp按钮
            actionBar.setDisplayShowTitleEnabled(false);//不自动显示标题
        }
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.my_request_refreshLayout);
        recyclerView = (RecyclerView) findViewById(R.id.my_request_recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new MyRequestDetailAdapter(list);
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new DividerItemDecoration(this,
                DividerItemDecoration.VERTICAL_LIST));
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_red_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_green_light,
                android.R.color.holo_blue_bright);
        swipeRefreshLayout.setOnRefreshListener(this);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                break;
        }
        return true;
    }

    @Override
    public void onRefresh() {
        //刷新
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //延迟4s后停止刷新
                swipeRefreshLayout.setRefreshing(false);
            }
        }, 4000);

            new Handler().post(new Runnable() {
                @Override
                public void run() {
                    refreshData();
                }
            });


    }
    private void refreshData(){
        NetUtil.getUserAllRequest(SharedPrefUtil.getUserPhone(),list,adapter);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (MyApplication.getDeletePosition()!=-1){//有请求被删除了
            list.remove(MyApplication.getDeletePosition());
            adapter.notifyItemRemoved(MyApplication.getDeletePosition());
            adapter.notifyItemRangeChanged(MyApplication.getDeletePosition(),list.size()-MyApplication.getDeletePosition()-1);
            SharedPrefUtil.saveReqCount(SharedPrefUtil.getReqCount()-1);
            MyApplication.setDeletePosition(-1);//还原
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SharedPrefUtil.saveReqCount(0);//可以重新进行刷新
    }
}